<!-- Add Modal //-->
<div id="addModal" class="reveal-modal medium" data-reveal>
	<span id="loadingKelas" style="display:none;">Sedang memproses..</span>
	<span id="errorkelas" style="display:none;color:red;">Ma'af, tidak dapat memuat</span>
	<div id="add_tholabah_modal"></div>
</div>
<!-- End Add Modal //-->

<!-- Delete Modal //-->
<div id="delModal" class="reveal-modal tiny" data-reveal>
	<p class="p-modal">
	<span id="hpid" class="hpid var-modal" style="display:none;"></span>
	Yakin ingin menghapus <span class="nama var-modal"></span> 
	kelas <span class="2nd var-modal"></span> 
	dari penerima <span class="3rd var-modal"></span> 
	sebesar <span class="4th var-modal"></span> 
	untuk tagihan <span class="5th var-modal"></span> 
	sebesar <span class="6th var-modal"></span>?
	<br/><br/>
	<span id="loadingDel" style="display:none;">Sedang memproses..</span>
	<span id="errorDel" style="display:none;color:red;">Ma'af, gagal dihapus</span>
	<span class="pull-right">
		<button class="btn btn-xs tmb-utama" id="HapusBagiSubNonSy"> Yakin</button> 
		<button class="btn tmb-kedua close-reveal-modal close-modal"> Tidak</button></span>
</div>
<!-- End Delete Modal //-->

<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default kotakwidget">
			<div class="panel-heading kepalakotakwidget">
				Subsidi Tagihan <?php foreach ($list_tag as $row) {$id_a_non_sy = $row->id_a_non_sy; $idjp = $row->id_m_j_pendidikan; echo $row->nama_non_sy.' bagi jenjang '.$jp = $row->j_pendidikan;?>
				<input type="hidden" id="jp" value="<?php echo $jp; ?>">
				<input type="hidden" id="id_a_non_sy" value="<?php echo $id_a_non_sy; ?>">
				<div>
					<span class="wali_kelas">
						Besar tagihan <?php echo formatRupiah($row->besar_bayar);?>. Masa Bayar: <?php echo hijriah($row->mulai_bayar).' sd '.hijriah($row->akhir_bayar); ?>.
						Sebanyak <span id="jml"><?php echo count($list);?></span> dari <?php echo $row->jml;}?> tholabah tertagih telah diberi subsidi
					</span>
				
					<span class="pull-right tmb_panel_head">
						<button data-reveal-id="addModal" onClick="add_tholabah('<?php echo $idjp;?>')" data-reveal class="btn btn-sm tmb-tambah tmb-utama"><i class="fa fa-plus"></i> Tambah</button>
					</span>	
				</div>			
			</div>
				
				<div class="panel-body">
					<div class="row">
						<div class="col-md-12">
							<div id="contentBagiSubNonSy">
							<table class="table table-condensed" id="th_ajaran">
							  <thead>
								<tr>
									<th width="30">#</th>
									<th width="80">NIT</th>
									<th>Nama Lengkap</th>
									<th>Jenis Kelamin</th>
									<th>Kelas</th>
									<th>Subsidi</th>
									<th width="123">Besar Subsidi</th>
									<th width="123">Sisa Tagihan</th>
									<th></th>
								</tr>
							  </thead>
							  <tbody>
								<?php 
									if(count($list)>0){
										$i = 1; 
										foreach ($list as $row) {
								?>
								<tr class="hover">
									<td><?php echo $i++ ?></td>
									<td><?php echo $row->id_m_tholabah;?></td> 
									<td><?php echo $row->nama_lengkap;?></td> 
									<td><?php echo $row->jk;?></td>
									<td><?php echo $row->kelas;?></td>
									<td><?php echo $row->nama_subsidi;?></td>
									<td><?php echo formatRupiahTabel($row->besar_subsidi);?></td>
									<td><?php echo formatRupiahTabel($row->sisa);?></td>
									<td>
										<a class="editorLink pull-right" data-reveal-id="delModal" nama="<?php echo $row->nama_lengkap; ?>" 
										2nd="<?php echo $row->kelas;?>" 
										3rd="<?php echo $row->nama_subsidi;?>" 
										4th="<?php echo formatRupiah($row->besar_subsidi);?>" 
										5th="<?php foreach ($list_tag as $row_tag) {echo $row_tag->nama_non_sy;}?>" 
										6th="<?php foreach ($list_tag as $row_tag) {echo formatRupiah($row_tag->besar_bayar);}?>" 
										hpid="<?php echo $row->id_b_sub_non_sy;?>" data-reveal>
										<span class="glyphicon glyphicon-floppy-remove"></span></a>
									</td>
								</tr>
								<?php
										}
									}else{
								?>
								<tr>
									<td colspan="9"><center>Belum ada tholabah <?php foreach ($list_tag as $row) {echo $row->j_pendidikan;}?> tersubsidi tagihan <?php foreach ($list_tag as $row) {echo $row->nama_non_sy;}?></center></td>
								</tr>
								<?php
									}
								?>
							  </tbody>
							</table>
							</div>
						</div>
					</div>
				</div>
		</div>
	</div>	
</div>
<!-- /#page-wrapper -->
<script>
// Menampilkan isi modal tambah
function add_tholabah(id_m_j_pendidikan){
	$.ajax({
	type: "POST",
		data: "id_m_j_pendidikan="+id_m_j_pendidikan,
		url : "<?php echo site_url(); ?>b_sub_non_sy/subsidi",				
		cache: false,
		beforeSend: function(){
			$('#loadingAdd').show();
			$('#errorAdd').hide();
		}, 
		success: function(response){
			$('#add_tholabah_modal').html(response);
			$('#loadingAdd').hide();	

			var jenjang = $("#jp").val();
			$('#j_pend').text(jenjang);		

			var id_a_non_sy = $("#id_a_non_sy").val();
			$('#id_ansy').val(id_a_non_sy); // Ambil value #id_a_non_sy 		
		}, 
		error:function(){
			$('#errorAdd').show();
			$('#loadingAdd').hide();
		}
	});
	return false;
}
// Ajax Complete
$(document).ajaxComplete(function(){
	// Format Rp
	$('#besar_subsidi,#sub,#sisa_sub').priceFormat({
	    prefix: 'Rp ',
	    centsLimit: '',
	    thousandsSeparator: '.'
	});
	// Combo pilih Subsidi
	$("#id_a_subsidi").change(function(){
		var id_a_subsidi = $("#id_a_subsidi").val();
		var id_m_j_pendidikan = $("#id_m_j_pendidikan").val();
		var id_a_non_sy = $("#id_ansy").val();
		$.ajax({
			type: "POST",				
			data: "id_a_subsidi="+id_a_subsidi+"&id_m_j_pendidikan="+id_m_j_pendidikan+"&id_a_non_sy="+id_a_non_sy,
			url : "<?php echo site_url(); ?>b_sub_non_sy/sisa_dan_tholabah",
			beforeSend: function(){
				$('#loading').show();
				$('#error').hide();
			},
			success: function(msg){
				$('#loading').hide();
				$('#sisa').html(msg);
				$('#besar_subsidi').focus();
				$('#id_a_non_sy2').val(id_a_non_sy);
			},
			error:function(){
				$('#loading').hide();
				$('#error').show();
			}
		});		
		stopPropagation();		
	});
	// Close alert/rincian subsidi
	$("#pilihLagi").click(function(){		
		$("#subsidi2").show('slow');
		$("#subsidi").show('slow');		
		$("#rincian").hide('slow');
		$('#besar_subsidi').focus();
	});
	// Enter pada textbox besar subsidi
	var code =null;
	$('#besar_subsidi').keypress(function(e){
	    code= (e.keyCode ? e.keyCode : e.which);
	    if (code == 13) {

			$("#subsidi").hide('slow');
			$("#subsidi2").hide('slow');
			$("#rincian").show('slow');	

			var besar_sub = $("#besar_subsidi").val();
			$('#sub').text(besar_sub); // Ambil value #besar_subsidi 
			
			var bes_sub = $("#besar_subsidi").unmask();
			$('#besar_sub').val(bes_sub); // Ambil value var bes_sub => #besar_subsidi 

			// Menghitung sisa subsidi dikurangi tholabah yang dichecked
	        var jmlChecked = $('input:checkbox:checked').length; // Jumlah yang dicheck
	        var besar_subsidi = $("#besar_subsidi").unmask(); // Menghilangkan format Rp
	        var sisa_subsidi = $("#sisa_subsidi").val();
	        var total = jmlChecked * besar_subsidi;
	        var sisa = sisa_subsidi - total;
	        $('#sisa_sub').text(sisa);

			// Format Rp
			$('#besar_subsidi,#sub,#sisa_sub').priceFormat({
			    prefix: 'Rp ',
			    centsLimit: '',
			    allowNegative: true,
			    thousandsSeparator: '.'
			});
		}
	});	

	// Checkbox dalam bentuk table row
    $('.diklik tr').click(function(event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }
    });

    $("input[type='checkbox']").change(function (e) {
        if ($(this).is(":checked")) {
            $(this).closest('tr').addClass("highlight_row");
        } else {
            $(this).closest('tr').removeClass("highlight_row");
        }
        //var numberNotChecked = $('input:checkbox:not(":checked")').length;
        // Menghitung sisa subsidi dikurangi tholabah yang dichecked
        var jmlChecked = $('input:checkbox:checked').length;
        var besar_sub = $("#besar_subsidi").unmask();
        var besar_subsidi = besar_sub;
        var sisa_subsidi = $("#sisa_subsidi").val();
        var total = jmlChecked * besar_subsidi;
        var sisa = sisa_subsidi - total;
  		$('#sisa_sub').text(sisa); 

  		// Format Rp
		$('#sisa_sub').priceFormat({
		    prefix: 'Rp ',
		    centsLimit: '',
		    allowNegative: true,
		    thousandsSeparator: '.'
		});    
		// Jika ada yang di checked dan sisa >= 0 maka tombol akan enabled/hilangkan disabled
  		var buttonsChecked = $('input:checkbox:checked');
	    if (buttonsChecked.length && sisa >= 0) {
	        $('#saveButton').removeAttr('disabled');
	    }else {
	    	$('#saveButton').attr('disabled', 'disabled');
	    }
    });
	// Save
	$('#SaveBagiSubNonSy').bind('submit', function(event) {
		var link = $(this).attr('action');
		var id_a_non_sy = $("#id_a_non_sy2").val();
		$.ajax({
			url: link,
			type: "POST",
			data: $(this).serialize(),
			cache: false,
			beforeSend: function(){
				$('#errorSave').hide();
				$('#loadingSave').show();
			},          
			success: function() {
				var $container = $("#contentBagiSubNonSy");
				$container.load("<?php echo site_url(); ?>b_sub_non_sy/refresh/"+id_a_non_sy);

				var $container = $("#jml");
				$container.load("<?php echo site_url(); ?>b_sub_non_sy/refresh_jml/"+id_a_non_sy);
				
				$('#loadingSave').hide();
				$('#addModal').foundation('reveal', 'close');
			},
			error:function(){
				$('#loadingSave').hide();
				$('#errorSave').show();
			}
		});
		return false;
	});
});
// Delete
$("#HapusBagiSubNonSy").on("click", function(){
	var id_b_sub_non_sy = $("#hpid").text();
	var id_a_non_sy = $("#id_a_non_sy").val();
	$.ajax({
		type: "POST",
		data: "id_b_sub_non_sy="+id_b_sub_non_sy,
		url : "<?php echo site_url(); ?>b_sub_non_sy/delete",				
		cache: false,
		beforeSend: function(){
			$('#loadingDel').show();
			$('#errorDel').hide();
		}, 
		success: function(){
			var $container = $("#contentBagiSubNonSy");
			$container.load("<?php echo site_url(); ?>b_sub_non_sy/refresh/"+id_a_non_sy);

			var $container = $("#jml");
			$container.load("<?php echo site_url(); ?>b_sub_non_sy/refresh_jml/"+id_a_non_sy);
			
			$('#loadingDel').hide();
			$('#delModal').foundation('reveal', 'close');					
		}, 
		error:function(){
			$('#loadingDel').hide();
			$('#errorDel').show();
		}
	});
	return false; 
});
</script>